package com.outdd.vaietool.module.story.service.impl;

import com.outdd.vaietool.common.enumerate.SourceEnum;
import com.outdd.vaietool.common.utils.ReptileUtil;
import com.outdd.vaietool.common.utils.convert.EncodingConvert;
import com.outdd.vaietool.module.story.dao.entity.StoryEntity;
import com.outdd.vaietool.module.story.service.StoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: StoryServiceImpl
 * @description:
 * @author: VAIE
 * @create: 2021-05-07 11:18
 **/
@Service
@Slf4j
public class StoryServiceImpl implements StoryService {

    @Override
    public String captureStory(String url,String source,String uuid) {
        SourceEnum sourceEnum = SourceEnum.getEnum(source);

        List<Document> fdList= ReptileUtil.getDirectoryAll(url,
                sourceEnum.getDirectoryRule(),
                sourceEnum.getIsAddHost(),sourceEnum.getSkipNum(),uuid);

        return ReptileUtil.DocumentToTxt(fdList,sourceEnum.getTitleRule(),sourceEnum.getContentsRule(),uuid);
    }

    @Override
    public List<StoryEntity> queryStory(String queryString) {

        List<StoryEntity> novelByUrlToList = queryStoryByQiDian(queryString);
        novelByUrlToList.addAll(queryStoryByQuBiGe(queryString));

        return novelByUrlToList;
    }

    public List<StoryEntity> queryStoryByQiDian(String queryString){
        String vallue = EncodingConvert.zhConvertUrl(queryString);
        String url = "https://www.qidian.com/search?kw="+vallue;
        Document doc = ReptileUtil.getDocumentOfHttps(url);
        List<StoryEntity> list = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(doc)) {
            //li
            Elements lis = doc.select(".res-book-item");
            for (Element li : lis) {
                StoryEntity story = new StoryEntity();

                //div
                Elements boxs = li.select(".book-img-box");
                for(Element box : boxs){
                    Elements urlAndTitles =box.select("a img");
                    Element element = urlAndTitles.get(0);
                    story.setImageUrl(element.attr("src"));
                }
                Elements infos = li.select(".book-mid-info");

                story.setIntroduce(ReptileUtil.parseElementsText(infos,".intro"));
                story.setLatestChapter(ReptileUtil.parseElementsText(infos,".update a"));
                Element h4_a = ReptileUtil.parseElements(infos, "h4 a", 0);
                story.setTitle(h4_a.text());
                story.setDownUrl("https:" + h4_a.attr("href"));

                Elements authors =infos.select(".author");
                story.setAuthor(ReptileUtil.parseElementsText(authors,"a"));
                story.setType(ReptileUtil.parseElementsText(authors,"a",1));
                story.setStatus(ReptileUtil.parseElementsText(authors,"span"));
                story.setSource(SourceEnum.QI_DIAN.getDesc());
                try {
                    Elements rInfos = li.select(".book-right-info");
                    story.setTotalWords(ReptileUtil.parseElementsText(rInfos,"p"));
                    story.setTotalRecommendNum(ReptileUtil.parseElementsText(rInfos,"p",1));
                }catch (Exception e){
                }

                list.add(story);
            }
        }
        return list;
    }

    public List<StoryEntity> queryStoryByQuBiGe(String queryString){
        String vallue = EncodingConvert.zhConvertUrl(queryString);
        String url = "https://www.biquges.com/modules/article/search.php";
        HashMap<String, String> map = new HashMap<>();
        map.put("searchtype","articlename");
        map.put("searchkey",queryString);
        Document doc = ReptileUtil.getDocumentOfPost(url,map,"Hm_lvt_3fa7113f227f782789d45fffed45a416=1620696290,1621409853; clickbids=18128; Hm_lpvt_3fa7113f227f782789d45fffed45a416=1622166694; cscpvcouplet9193_fidx=1; cscpvrich9192_fidx=1");
        List<StoryEntity> list = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(doc)) {
            //li
            Elements lis = doc.select("tbody #nr");
            for (Element li : lis) {
                StoryEntity story = new StoryEntity();

                Element h4_a = ReptileUtil.parseElement(li, ".odd a", 0);
                story.setTitle(h4_a.text());
                story.setDownUrl("https://www.biquges.com" + h4_a.attr("href"));
                story.setLatestChapter(ReptileUtil.parseElementText(li,".even a"));
                story.setSource(SourceEnum.QU_BI_GR.getDesc());
                story.setTotalWords(ReptileUtil.parseElementText(li,".even",1));
                story.setStatus(ReptileUtil.parseElementText(li,".even",2));
                story.setAuthor(ReptileUtil.parseElementText(li,".odd",1));
                list.add(story);
            }
        }
        return list;
    }

}
